package com.zkca.xf.ui.activity.technicalprotect;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.videogo.constant.IntentConsts;
import com.videogo.exception.BaseException;
import com.videogo.openapi.EZOpenSDK;
import com.videogo.openapi.bean.EZCameraInfo;
import com.videogo.openapi.bean.EZDeviceInfo;
import com.zkca.xf.Constants_SP;
import com.zkca.xf.MyApplication;
import com.zkca.xf.R;
import com.zkca.xf.base.BaseTitleActivity;
import com.zkca.xf.model.javabean.VideoiList;
import com.zkca.xf.presenter.VideoPersenter;
import com.zkca.xf.ui.adapter.SingleAdapter;
import com.zkca.xf.ui.adapter.ViewHolder;
import com.zkca.xf.ui.activity.yingshi.EZRealPlayActivity;
import com.zkca.xf.ui.activity.yingshi.EZUtils;
import com.zkca.xf.utils.SPUtil;
import com.zkca.xf.view.VideoView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class VideoActivity extends BaseTitleActivity implements VideoView {

    @BindView(R.id.xrv_erc)
    XRecyclerView xrvVideo;

    private VideoPersenter videoPersenter;

    private LinearLayoutManager linearLayoutManager;
    private SingleAdapter<VideoiList.DataBean> adapter;
    private List<VideoiList.DataBean> dataBeanList;
    private EZOpenSDK mEZOpenSDK;
    private String xuliehao;
    private List<String> xuliehaolist=new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        ButterKnife.bind(this);

        initview();
        initData();
    }

    private void initData() {
        videoPersenter = new VideoPersenter(this, this);
        Map map = new HashMap<>();
        map.put("userId", SPUtil.getString(this,Constants_SP.USER_ID, ""));
        videoPersenter.getVideoList(map);
    }

    private void initview() {
        setTitleText("视频监控");
        setleftTextVisibility(true);


        mEZOpenSDK = MyApplication.getOpenSDK();
        dataBeanList = new ArrayList<>();
        linearLayoutManager = new LinearLayoutManager(this);
        xrvVideo.setLayoutManager(linearLayoutManager);
        adapter = new SingleAdapter<VideoiList.DataBean>(this, dataBeanList, R.layout.video_item) {
            @Override
            public void convert(ViewHolder holder, final VideoiList.DataBean dataBean, final int position) {
                TextView videoname = holder.getView(R.id.camera_name_tv);
                videoname.setText(dataBean.getBuildName());

                String manuFacturer = dataBean.getManuFacturer();
                String url = manuFacturer;
                Log.e("播放地址为", "<>" + url+"()()()"+dataBean.getBuildName());//ezopen://open.ys7.com/781794521/1.live

                String substring = url.substring(url.indexOf("/") + 1, url.lastIndexOf("."));
                String[] split = substring.split("/");
                xuliehao = split[split.length - 2];


                xuliehaolist.add(xuliehao);
                holder.setItemViewOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    List<EZDeviceInfo> result = null;
                                    result = mEZOpenSDK.getDeviceList(0, 10);
                                    EZDeviceInfo deviceInfo = mEZOpenSDK.getDeviceInfo(xuliehaolist.get(position));
                                    final EZCameraInfo cameraInfo = EZUtils.getCameraInfoFromDevice(result.get(0), 0);

                                    Intent intent1 = new Intent(VideoActivity.this, EZRealPlayActivity.class);
                                    intent1.putExtra(IntentConsts.EXTRA_CAMERA_INFO, cameraInfo);
                                    intent1.putExtra(IntentConsts.EXTRA_DEVICE_INFO, result.get(0));
                                    intent1.putExtra("xuliehao",  xuliehaolist.get(position));
                                    Log.e("序列号", dataBean.getBuildName()+"<>" + xuliehaolist.get(position));
                                    startActivityForResult(intent1, 100);
                                    //finalViewHolder.playBtn.setClickable(true);
                                    return;

                                } catch (BaseException e) {

                                    e.printStackTrace();
                                }
                            }
                        }).start();

                    }
                });
            }
        };


        xrvVideo.setAdapter(adapter);
    }

    @Override
    public void showProgressDialog() {
        showSpinKitLoading(true);
    }

    @Override
    public void hideProgressDialog() {
        showSpinKitLoading(false);
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void getVideolist(VideoiList videoList) {
        List<VideoiList.DataBean> data = videoList.getData();

        if (data != null && data.size() > 0) {
            dataBeanList.addAll(data);
        }

        adapter.notifyDataSetChanged();

    }
}
